/**
 * 电子单中间缩略图控制逻辑
 * @author: yup
 * @time: 2016/2/1
 *
 */

//变量定义
var imageInfo = null;
var imageId = null;
var patId = 0;
var studyUID = 0;
var dcmurl=null;
var serverURL=null;

//影像单静态图片显示变量
var showFlag =0; //控制页面显示几个图的变量 (1:显示1张，2：显示2张，3：显示20张)
var instanceCount = 0;  //影像数目
var series =new Array(); //序列数组
var instanceImageUrl = []; //单图访问url数组

// Get configuration parameters
var queryString = window.location.href;
var hostport = document.location.host;
hosturl = 'http://' + hostport;
var patientName = '';


$(function(){
	imageId = getParameter(queryString, "imageid");
//	imageid =303 (2个) ，416（1个）385（10个）306 41个
//	imageId = "306";

	$.ajax({
		url : hosturl + '/idoctor/service/ImageViewer/getConfig',
		type : 'get',
		data : {},
		dataType : 'json',
		async: false,
		success : function(data) {
			var urls = data.message.split(";");
			// load image
			dcmurl = urls[0];
			serverURL = urls[1];
		},
		error : function(data) {
			humane.log("服务器连接错误，请联系客服。", {
				timeout : 4000,
				clickToClose : true,
				addnCls : 'humane-error'
			});
		}
	});

	// get image information
	$.ajax({
		url : hosturl + '/idoctor/service/image/' + imageId,
		type : 'get',
		dataType : 'json',
		async: false,
		success : function(data) {
			var imageInfo = JSON.parse(data.message);
			patId = imageInfo.patientid;
			studyUID = imageInfo.studyid;
			patientName = imageInfo.patientname;
			//读图加载影像
			getStudy(hosturl, patId, studyUID, dcmurl,serverURL);

//			for(var j=0 ;j< instanceImageUrl.length; j++){
//				console.log("wado url length : " + instanceImageUrl.length);
//				console.log("wado url : " + instanceImageUrl[j]);
//			}
		},
		error : function(data) {
			humane.log("影像服务器连接错误。Cannot connect server to get the series information.",
			{
				timeout : 1000,
				clickToClose : true,
				addnCls : 'humane-error'
			});
		}
	});

	//电子单图像显示方法
	displayImage();

	//电子单缩略图影像单 点击到具体的影像
	$("#next").on("click",function(){
		window.location.href = hosturl + "/idoctor/html/imageeform.html?imageid= " + imageId;
	});

});

//取Study对象
function getStudy(hosturl, patId, studyUID, dcmurl, serverurl) {
	$.ajax({
			url : hosturl + '/idoctor/service/series/getseries',
			type : 'get',
			data : {
				patientid : patId,
				studyid : studyUID,
				dcmURL : dcmurl
			},
			dataType : 'json',
			async:false,
			success : function(data) {
				var message = JSON.parse(data.message);
				iSeriesNum = message.length;

				if (iSeriesNum > 0) {
					series = message;
					totalSeries = iSeriesNum;

					for (var i = 0; i < iSeriesNum; i++) {
						// 取序列下的影像
						//getInstance 具体得到影像单图的url
						getInstances(hosturl, patId, studyUID,
								series[i].seriesIUID, dcmurl, serverurl, i);
						//将多个 series 的具体instance 图像相加
						instanceCount = instanceCount + series[i].numberOfInstances;

					}
					//根据影像数目判断1、3、20张显示标识
					if(instanceCount ==1){
						showFlag =1;
					}else if(instanceCount >1 && instanceCount <20){
						showFlag =2;
					}else if(instanceCount >= 20){
						showFlag = 3;
					}

				} else {
					humane.log("影像服务器连接错误。Cannot connect server to get the study.");
				}
			},
			error : function(data) {
				humane.log("影像服务器连接错误。Cannot connect server to get the study.");
			}
		});
}

//取序列影像
function getInstances(hosturl, patId, studyUID, seriesUID, dcmurl, serverurl, index) {
	//单图访问url
	var wadoURL = hosturl + "/idoctor/read?" + "serverURL=" + serverurl +  "/wado&study=" + studyUID + "&series=" + seriesUID;

	$.ajax({
			url : hosturl + '/idoctor/service/instances/getinstances',
			type : 'get',
			data : {
				hostURL : hosturl,
				patientid : patId,
				studyid : studyUID,
				seriesid : seriesUID,
				dcmURL : dcmurl,
				serverURL : serverurl
			},
			dataType : 'json',
			async: false,
			success : function(data) {
				var instancesObj = JSON.parse(data.message);
				//组装需要显示的img 数组
				for(var i =0;i < instancesObj.length;i++){
					var instance = instancesObj[i];
					var urlTmp = wadoURL + "&object=" + instance.sopIUID + "&rows=120";
					//数组添加具体的图像url
					instanceImageUrl.push(urlTmp);
				}

			},
			error : function(data) {
				humane.log("影像服务器连接错误。Cannot connect server to get the series information.",
					{
						timeout : 4000,
						clickToClose : true,
						addnCls : 'humane-error'
					});
			}
		});
}

//电子单图像显示方法
function displayImage(){
	//根据变量showFlag 判断显示1、2、20 张图的页面div
	if(showFlag ==1){
		$("#oneimg").css("display","block");
		$("#twoimg").css("display","none");
		$("#twentyimg").css("display","none");

		//显示图
		if(instanceImageUrl.length >0){
			//具体显示图
			$('.contentoneimg').css('background','url(\'' + instanceImageUrl[0] + '\') no-repeat');
		}

	}else if(showFlag ==2){
		$("#oneimg").css("display","none");
		$("#twoimg").css("display","block");
		$("#twentyimg").css("display","none");
		//2张图具体显示
		if(instanceImageUrl.length >0){
//			for(var i = 0 ; i < instanceImageUrl.length; i++){
				$('.contenttwoimgtwo1').css('background','url(\'' + instanceImageUrl[0] + '\') no-repeat');
				$('.contenttwoimgtwo2').css('background','url(\'' + instanceImageUrl[1] + '\') no-repeat');

//			}
		}

	}else if(showFlag ==3){
		$("#oneimg").css("display","none");
		$("#twoimg").css("display","none");
		$("#twentyimg").css("display","block");
		//20张图具体显示
		if(instanceImageUrl.length >=20){
			$('.twentyimg1').css('background','url(\'' + instanceImageUrl[0] + '\') no-repeat');
			$('.twentyimg2').css('background','url(\'' + instanceImageUrl[1] + '\') no-repeat');
			$('.twentyimg3').css('background','url(\'' + instanceImageUrl[2] + '\') no-repeat');
			$('.twentyimg4').css('background','url(\'' + instanceImageUrl[3] + '\') no-repeat');
			$('.twentyimg5').css('background','url(\'' + instanceImageUrl[4] + '\') no-repeat');
			$('.twentyimg6').css('background','url(\'' + instanceImageUrl[5] + '\') no-repeat');
			$('.twentyimg7').css('background','url(\'' + instanceImageUrl[6] + '\') no-repeat');
			$('.twentyimg8').css('background','url(\'' + instanceImageUrl[7] + '\') no-repeat');
			$('.twentyimg9').css('background','url(\'' + instanceImageUrl[8] + '\') no-repeat');
			$('.twentyimg10').css('background','url(\'' + instanceImageUrl[9] + '\') no-repeat');
			$('.twentyimg11').css('background','url(\'' + instanceImageUrl[10] + '\') no-repeat');
			$('.twentyimg12').css('background','url(\'' + instanceImageUrl[11] + '\') no-repeat');
			$('.twentyimg13').css('background','url(\'' + instanceImageUrl[12] + '\') no-repeat');
			$('.twentyimg14').css('background','url(\'' + instanceImageUrl[13] + '\') no-repeat');
			$('.twentyimg15').css('background','url(\'' + instanceImageUrl[14] + '\') no-repeat');
			$('.twentyimg16').css('background','url(\'' + instanceImageUrl[15] + '\') no-repeat');
			$('.twentyimg17').css('background','url(\'' + instanceImageUrl[16] + '\') no-repeat');
			$('.twentyimg18').css('background','url(\'' + instanceImageUrl[17] + '\') no-repeat');
			$('.twentyimg19').css('background','url(\'' + instanceImageUrl[18] + '\') no-repeat');
			$('.twentyimg20').css('background','url(\'' + instanceImageUrl[19] + '\') no-repeat');

		}
	}
	//显示影像姓名 /ID
	$('#patientname').html('【' +patientName +'&nbsp;ID:' + patId +'】');
}

//load the parameter from querystring
function getParameter(queryString, parameterName) {
	// Add "=" to the parameter name (i.e. parameterName=value);
	var parameterName = parameterName + "=";
	if (queryString.length > 0) {
		// Find the beginning of the string
		var begin = queryString.indexOf(parameterName);
		if (begin != -1) {
			// Add the length (integer) to the beginning
			begin += parameterName.length;
			var end = queryString.indexOf("&", begin);
			if (end == -1) {
				end = queryString.length;
			}
			return unescape(queryString.substring(begin, end));
		}
		return null;
	}
}






